package com.hxgis.oc.ncep;

import com.hxgis.oc.config.OConfig;
import com.hxgis.oc.ncep.base.NcepBase;
import com.hxgis.oc.product.file.DownloadTask;
import com.hxgis.oc.util.TimerBase;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;

@Slf4j
public class NcepTimer1h extends TimerBase {
    public static void start(NcepBase request) {
        TimerBase.start(NcepTimer1h.class.getSimpleName(),new NcepTimer1h(request));
    }

    private final NcepBase request;

    @Override
    protected DownloadTask createATask(long fid, File outputFile) {
        return request.createATask(fid,outputFile);
    }

    public NcepTimer1h(NcepBase req) {
        super(req.getDataSet(),1,0);
        request = req;
        isUTC = true;
        neverExpired = true;
    }

    @Override
    public void run() {
        super.run();

        if(!isTimeToUpdate()){
            return;
        }

        ArrayList<LocalDateTime> queryTimes = genQueryTimes();

        if(queryTimes.isEmpty())
        {
            return;
        }

        LocalDateTime startDataTime = queryTimes.get(queryTimes.size() - 1);
        long fileCount = fileProductManager.count(dataSet,startDataTime);

        if(queryTimes.size() == fileCount)
        {
            return;
        }

        long taskCount = 0;
        for(LocalDateTime time : queryTimes)
        {
            long fid =  OConfig.genTimeStampFid(time);
            File file = fileProductManager.getFile(dataSet,request.generateOutputFileName(fid));
            DownloadTask task = createATask(fid,file);
            if(!fileProductManager.exist(dataSet,fid)){
                fileProductManager.getWorker().addTask(task);
                taskCount++;
            }
        }

        log.info("[{} {}至现在] :已下载{} 队列{}", dataSet,OConfig.formatLogString(startDataTime,isUTC),fileCount,taskCount);
    }

    ArrayList<LocalDateTime> genQueryTimes(){
        ArrayList<LocalDateTime> generateTimes = new ArrayList<>();
        int currentHour = timeCurrent.getHour();

        //UTC 00,06,12,18

        int todayNum = 1;
        LocalDateTime nearDataTime;
        if(currentHour >= 18){
            nearDataTime = timeCurrent.minusHours(currentHour - 18);
            todayNum = 4;
        }
        else if(currentHour >= 12){
            nearDataTime = timeCurrent.minusHours(currentHour - 12);
            todayNum = 3;
        }
        else if(currentHour >= 6){
            nearDataTime = timeCurrent.minusHours(currentHour - 6);
            todayNum = 2;
        }
        else{
            nearDataTime = timeCurrent.minusHours(currentHour);
            todayNum = 1;
        }

        //最近一个更新有延迟？
        nearDataTime = nearDataTime.minusHours(6);
        todayNum--;

        //最近2天数据
        int dayNum = 2;
        int size = todayNum + 4 * dayNum;

        for(int i=0;i<size;i++){
            generateTimes.add(nearDataTime.minusHours(i * 6));
        }

        return generateTimes;
    }
}
